package leetcode.list_problem;

/**
 * 剑指 Offer II 022. 链表中环的入口节点
 * https://leetcode-cn.com/problems/c32eOV/
 */
public class DetectCycle {
    public ListNode detectCycle(ListNode head) {
        ListNode low = head;
        ListNode fast = head;
        while (fast != null && fast.next != null){
            fast = fast.next.next;
            low = low.next;
            if (fast == low){
                ListNode node = head;//这个方法贼巧妙多看看
                while (node != low){//这里会先来个判断免得直接执行第一次会跳
                    node = node.next;
                    low = low.next;
                }
                return node;
            }
        }
        return null;
    }
}
